$(function (ev) {
    //选项卡
    $(".tab li").click(function (ev) {
        var index = $(this).index();
        $(".view li").each(function (i, elem) {
            $(elem).css({
                display: "none"
            });

        });
        $(this).addClass("active").siblings().removeClass("active");
        $(".view li").eq(index).css({
            display: "block"
        });
    });
    //初始化刷新
    refreshNM(1);
    hotMusic();
    refreshNM(0);
//  最新添加播放
    $("#newMusic a").eq(0).click(function () {
        var addition = [];
        $("#newMusic input:checkbox[name='newM']:checked").each(function (i) {
            addition.push({"id":$("#newMusic input:checkbox[name='newM']:checked").eq(i).attr('id')});
        })
        // console.log(addition);
        $.ajax({
            type: "post",
            contentType: "application/json",
            url: "date/hot.json",
            data: addition,
            dataType: "text",
            success: function (data) {
                info([{
                    "p":"提交成功"
                }]);
                refreshNM(0);
                refreshNM(1);
            },
            error:function () {
                info([{
                    "p":"提交失败"
                }]);
            }
        })
    });
// 最新点歌删除
    $("#newMusic a").eq(1).click(function () {
        var Delete = [];
        $("#newMusic input:checkbox[name='newM']:checked").each(function (i) {
            Delete.push({"id":$("#newMusic input:checkbox[name='newM']:checked").eq(i).attr('id')});
        })
        console.log(Delete);
        $.ajax({
            type: "post",
            contentType: "application/json",
            url: "date/hot.json",
            data: Delete,
            dataType: "text",
            success: function (data) {
                info([{
                    "p":"提交成功"
                }]);
                refreshNM(0);
                refreshNM(1);
            },
            error:function () {
                info([{
                    "p":"提交失败"
                }]);
            }
        })
    });
//批量添加播放
    $("#hotMusic a").eq(0).click(function () {
        var addition = [];
        $("#hotTable input:checkbox[name='newM']:checked").each(function (i) {
            addition.push({"id":$("#hotTable input:checkbox[name='hoTm']:checked").eq(i).attr('id')});
        })
        console.log(addition);
        $.ajax({
            type: "post",
            contentType: "application/json",
            url: "date/hot.json",
            data: addition,
            dataType: "text",
            success: function (data) {
                info([{
                    "p":"提交成功"
                }]);
                refreshNM(0);
                refreshNM(1);
            },
            error:function () {
                info([{
                    "p":"提交失败"
                }]);
            }
        })
    });
    //今日播放的批量删除
    $("#todayMBtn a").eq(0).click(function () {

        var Delete = [];
        $("#todayM input:checkbox[name='newM']:checked").each(function (i) {
            Delete.push({"id":$("#todayM input:checkbox[name='newM']:checked").eq(i).attr('id')});
        });

        console.log(Delete);
        $.post("date/hot.json",Delete)
        $.ajax({
            type: "post",
            contentType: "application/json",
            url: "date/hot.json",
            data: Delete,
            dataType: "text",
            success: function (data) {
                info([{
                    "p":"提交成功"
                }]);
                refreshNM(0);
                refreshNM(1);
            },
            error:function () {
                info([{
                    "p":"提交失败"
                }]);
            }
        })

    })


    // 动态添加最新点歌
    function refreshNM(sign) {

        if (sign == 1) {
            $("#newTable tr").not("#newTable tr:first").remove();
        } else {
            $("#todayM tr").not("#todayM tr:first").remove();
        }

        var aStr;
        if (sign == 1) {
            aStr = "添加到播放";
        } else {
            aStr = "删除";
        }
        $.ajax({
            type: "get",
            contentType: "application/json?sign=" + sign,
            url: "date/nm.json",
            dataType: "text",
            success: function (data) {
                info([{
                    "p":"刷新成功"
                }]);
                data = eval('(' + data + ')');
                for (var i in data) {
                    var dataJson = data[i];
                    var newTr = $('<tr></tr>');
                    newTr = newTr.append($(' <td><input type="checkbox" id="' + dataJson.id + '" name="newM" "></td>'))
                    newTr = newTr.append($('<td><span>' + dataJson.from + '</span></td><td><span>' + dataJson.to + '</span></td><td><span>' + dataJson.musicName + '</span></td><td><p>' + dataJson.message + '</p></td>'));

                    var aTr = $('<td></td>')
                    var a = $('<a href="javascript:void(0)" title="' + dataJson.id + '">' + aStr + '</a>')
                        .click(function () {
                            console.log($(this).attr("title"));
                            if (sign == 1) {
                                $.post('date/hot.json', {id: $(this).attr("title")}, function () {
                                    refreshNM(1);
                                })
                            } else {
                                $.post('date/hot.json', {id: $(this).attr("title")}, function () {
                                    refreshNM(0);
                                    refreshNM(1);
                                })
                            }

                        })
                    aTr.append(a);
                    newTr.append(aTr);
                    if (sign == 1) {
                        $("#newTable").append(newTr);
                    } else {
                        $("#todayM").append(newTr);
                    }

                }
            },
            error: function (response) {
                info([{"p":"刷新失败"}]);
            }
        })
    }

    // 动态添加最热歌曲
    function hotMusic() {
        $.ajax({
            type: "get",
            contentType: "application/json",
            url: "date/hot.json",
            dataType: "text",
            success: function (data) {
                info([{"p":"刷新成功"}]);
                data = eval('(' + data + ')');
                console.log(data);
                for (var i in data) {
                    var dataJson = data[i];
                    var newTr = $('<tr></tr>');
                    newTr.append($('<td> <input name="hotM" type="checkbox" id="' + dataJson.id + '"></td>'));
                    newTr.append($('<td><span>' + dataJson.ranking + '</span></td>'));
                    newTr.append($('<td><span>' + dataJson.musicName + '</span></td>'));
                    newTr.append($('<td><span>' + dataJson.hot + '</span></td>'));
                    var aTr = $('<td></td>');
                    var a = $('<a href="javascript:void(0)" title=' + dataJson.id + '>添加</a>')
                        .click(function () {
                            console.log($(this).attr('title'));
                            $.post('date/hot.json', {id: $(this).attr("title")}, function (data) {
                                console.log("提交成功");
                            })
                        });
                    aTr.append(a);
                    newTr.append(aTr)
                    $("#hotTable").append(newTr);
                }
            },
            error: function (response) {
               info([{"p":"刷新失败"}])
            }
        })
    }


    // 公告类型
    var clickNum = 0, clickNum2 = 0, clickNum3 = 0;
    var announcebtn = $("#announcebtn");
    var tips = $("#tips");
    var activity = $("#activity");
    $("#announcebtn").click(function () {
        clickNum++;
        if (clickNum % 2 == 0) {
            announcebtn.css('background-color', '#659bb6')
        } else {
            announcebtn.css('background-color', '#15648b')
        }
    });

    $("#tips").click(function () {
        clickNum2++;
        if (clickNum2 % 2 == 0) {
            tips.css('background-color', '#659bb6')
        } else {
            tips.css('background-color', '#15648b')
        }
    });

    $("#activity").click(function () {
        clickNum3++;
        if (clickNum3 % 2 == 0) {
            activity.css('background-color', '#659bb6')
        } else {
            activity.css('background-color', '#15648b')
        }
    });

    /*
    ================================
    传入为一个json格式的文件如
       {
       "p":"xuyanxu"
       }
    ==================================
     */


    function info(data) {
        var oInContent = $("#incontent");
        for (var i  in data) {
            for (var key in data[i]) {
                oInContent.append('<' + key + '>' + data[i][key] + '');
            }
        }
        $("#info").stop();
        $("#info").animate({
            "right": 50 + "px"
        }, 800, "swing", function () {

        setTimeout(function () {
                $("#info").animate({
                    "right": -300 + "px"
                }, 800, "swing", function () {
                    $("#incontent").empty();
                });
            }, 2000);
        })
        // $("#info").attr("time",attrTime);

    }

})
